package softtech.apps.autoanswerphonecall;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;

import com.google.android.gms.common.data.e;
import com.google.android.gms.internal.db;
import com.google.android.gms.internal.is;

import softtech.apps.model.DatabaseHandler;
import softtech.apps.model.Timer;

import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.app.TimePickerDialog.OnTimeSetListener;
import android.content.Context;
import android.content.DialogInterface;
import android.content.res.Resources;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.view.View.OnKeyListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.NumberPicker;
import android.widget.Toast;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.TimePicker.OnTimeChangedListener;
import android.widget.ToggleButton;

public class AddSchedule extends ActionBarActivity {

	private Button btCancel, btSave;
	private CheckBox cbMonday, cbTuesday, cbWednesday, cbThurday, cbFriday,
			cbSaturday, cbSunday, cbAll;
	private ToggleButton toggleActivity, toggleRepeat;

	private TextView tvMon, tvTue, tvWed, tvThur, tvFri, tvSat, tvSun, tvTime,
			tvDay;

	private boolean isRepeat, isActivity, isMon, isTue, isWed, isThur, isFri,
			isSat, isSun, isAll;

	private List<String> listTime;

	private ListView lvTimer;

	private Context mContext;

	private String timeStart, timeEnd, dates;

	private ItemTimeTask adapter;

	private static int typeSchedule = 0;

	private DatabaseHandler dbHandler;

	private Timer newTask = null;

	// private Comparator<String> comTimer= new Comparator<String>() {
	//
	// @Override
	// public int compare(String lhs, String rhs) {
	// // TODO Auto-generated method stub
	//
	// String timeL[] = lhs.split(" - ");
	//
	// String lTL = timeL[0];
	//
	// String hm[] = lTL.split(":");
	//
	// if(Integer.valueOf(hm[0]) < 10){
	// hm[0] = "0" + hm[0];
	// }
	//
	// if(Integer.valueOf(hm[1]) < 10){
	// hm[1] = "0" + hm[1];
	// }
	//
	// lTL = hm[0] + ":" + hm[1];
	//
	//
	// String rTL = timeL[1];
	//
	// String rm[] = rTL.split(":");
	//
	// if(Integer.valueOf(rm[0]) < 10){
	// rm[0] = "0" + rm[0];
	// }
	//
	// if(Integer.valueOf(rm[1]) < 10){
	// rm[1] = "0" + rm[1];
	// }
	//
	// rTL = rm[0] + ":" + rm[1];
	//
	// lhs = lTL + " - " + rTL;
	//
	//
	// String timeR[] = rhs.split(" - ");
	//
	// lTL = timeR[0];
	//
	// String rtm[] = lTL.split(":");
	//
	// if(Integer.valueOf(rtm[0]) < 10){
	// rtm[0] = "0" + rtm[0];
	// }
	//
	// if(Integer.valueOf(rtm[1]) < 10){
	// rtm[1] = "0" + rtm[1];
	// }
	//
	// lTL = rtm[0] + ":" + rtm[1];
	//
	//
	// String rrTL = timeL[1];
	//
	// String rrm[] = rrTL.split(":");
	//
	// if(Integer.valueOf(rrm[0]) < 10){
	// rrm[0] = "0" + rrm[0];
	// }
	//
	// if(Integer.valueOf(rrm[1]) < 10){
	// rrm[1] = "0" + rrm[1];
	// }
	//
	// rrTL = rrm[0] + ":" + rrm[1];
	//
	// rhs = lTL + " - " + rrTL;
	//
	// Log.e("TRAI ", lhs + " PHAI " + rhs);
	//
	// return lhs.compareToIgnoreCase(rhs);
	// }
	// };

	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_add_schedule);

		mContext = getApplicationContext();

		findViewByid();

		dbHandler = new DatabaseHandler(mContext);

		listTime = new ArrayList<String>();

		typeSchedule = getIntent().getIntExtra("requestCode",
				MainActivity.REQUEST_CODE_ADD_SCHEDULE);

		if (typeSchedule == MainActivity.REQUEST_CODE_EDT_SCHEDULE) {

			// get Object to edit
			int idTimer = getIntent().getIntExtra("TIMER_ID", -1);

			if (idTimer == -1) {
				AlertDialog.Builder builder = new AlertDialog.Builder(mContext);

				builder.setTitle("Error !")
						.setMessage(
								"Somethings in task not correct. Please remove this task and create a new task to replace.")
						.setPositiveButton("Ok",
								new DialogInterface.OnClickListener() {

									@Override
									public void onClick(DialogInterface dialog,
											int which) {
										// TODO Auto-generated method stub
										finish();
									}
								});

				Dialog dialog = builder.create();
				dialog.show();
			} else {
				newTask = dbHandler.getTimer(idTimer);

				getActionBar().setTitle(newTask.getNameTask());

				String time[] = newTask.getTime().split("__");
				for (String t : time) {
					listTime.add(t);
				}

				isActivity = newTask.getActive() >= 1 ? true : false;

				isRepeat = newTask.getRepeat() >= 1 ? true : false;

				String date = newTask.getDates();

				if (date.contains("Mon")) {
					isMon = true;
				}
				if (date.contains("Tue")) {
					isTue = true;
				}
				if (date.contains("Wed")) {
					isWed = true;
				}
				if (date.contains("Thu")) {
					isThur = true;
				}
				if (date.contains("Fri")) {
					isFri = true;
				}
				if (date.contains("Sat")) {
					isSat = true;
				}
				if (date.contains("Sun")) {
					isSun = true;
				}

				if (isMon && isTue && isWed && isThur && isFri && isSat
						&& isSun) {
					isAll = true;
				}
			}

		}

		changeStatusOfDate();

		if (isActivity) {
			toggleActivity.setChecked(true);
		} else {
			toggleActivity.setChecked(false);
		}

		if (isRepeat) {
			toggleRepeat.setChecked(true);
		} else {
			toggleRepeat.setChecked(false);
		}

		adapter = new ItemTimeTask();

		Collections.sort(listTime);

		lvTimer.setAdapter(adapter);

		tvTime.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub

				View timePickerView = View.inflate(getApplicationContext(),
						R.layout.time_picker, null);

				final TimePicker timePickerStart = (TimePicker) timePickerView
						.findViewById(R.id.timePickerStart);

				
				View hourStart = timePickerStart.findViewById(Resources.getSystem().getIdentifier("hour", "id", "android"));
				EditText inputHStart = (EditText) hourStart.findViewById(Resources.getSystem().getIdentifier("numberpicker_input", "id", "android"));
				
				inputHStart.addTextChangedListener(new TextWatcher() {
					
					@Override
					public void onTextChanged(CharSequence s, int start, int before, int count) {
						// TODO Auto-generated method stub
						
					}
					
					@Override
					public void beforeTextChanged(CharSequence s, int start, int count,
							int after) {
						// TODO Auto-generated method stub
						
					}
					
					@Override
					public void afterTextChanged(Editable s) {
						// TODO Auto-generated method stub
						timePickerStart.setCurrentHour(Integer.valueOf(s.toString()));
					}
				});
				
				View minuteStart = timePickerStart.findViewById(Resources.getSystem().getIdentifier("minute", "id", "android"));
				EditText inputMStart = (EditText) minuteStart.findViewById(Resources.getSystem().getIdentifier("numberpicker_input", "id", "android"));
				inputMStart.addTextChangedListener(new TextWatcher() {
					
					@Override
					public void onTextChanged(CharSequence s, int start, int before, int count) {
						// TODO Auto-generated method stub
						
					}
					
					@Override
					public void beforeTextChanged(CharSequence s, int start, int count,
							int after) {
						// TODO Auto-generated method stub
						
					}
					
					@Override
					public void afterTextChanged(Editable s) {
						// TODO Auto-generated method stub
						timePickerStart.setCurrentMinute(Integer.valueOf(s.toString()));
					}
				});
				
				
				final TimePicker timePickerEnd = (TimePicker) timePickerView
						.findViewById(R.id.timePickerEnd);
				
				View hourEnd = timePickerEnd.findViewById(Resources.getSystem().getIdentifier("hour", "id", "android"));
				EditText inputHEnd = (EditText) hourEnd.findViewById(Resources.getSystem().getIdentifier("numberpicker_input", "id", "android"));
				
				inputHEnd.addTextChangedListener(new TextWatcher() {
					
					@Override
					public void onTextChanged(CharSequence s, int start, int before, int count) {
						// TODO Auto-generated method stub
						
					}
					
					@Override
					public void beforeTextChanged(CharSequence s, int start, int count,
							int after) {
						// TODO Auto-generated method stub
						
					}
					
					@Override
					public void afterTextChanged(Editable s) {
						// TODO Auto-generated method stub
						timePickerEnd.setCurrentHour(Integer.valueOf(s.toString()));
					}
				});
				
				View minuteEnd = timePickerEnd.findViewById(Resources.getSystem().getIdentifier("minute", "id", "android"));
				EditText inputMEnd = (EditText) minuteEnd.findViewById(Resources.getSystem().getIdentifier("numberpicker_input", "id", "android"));
				inputMEnd.addTextChangedListener(new TextWatcher() {
					
					@Override
					public void onTextChanged(CharSequence s, int start, int before, int count) {
						// TODO Auto-generated method stub
						
					}
					
					@Override
					public void beforeTextChanged(CharSequence s, int start, int count,
							int after) {
						// TODO Auto-generated method stub
						
					}
					
					@Override
					public void afterTextChanged(Editable s) {
						// TODO Auto-generated method stub
						timePickerEnd.setCurrentMinute(Integer.valueOf(s.toString()));
					}
				});
				
				timePickerStart.setIs24HourView(true);
				timePickerEnd.setIs24HourView(true);
				
				Calendar calendar = Calendar.getInstance();

				if (listTime.isEmpty()) {

					timeStart = String.valueOf(calendar
							.get(Calendar.HOUR_OF_DAY))
							+ ":"
							+ String.valueOf(calendar.get(Calendar.MINUTE));

					
				} else {
					String tim[] = listTime.get(listTime.size() - 1).split(
							" - ");

					timeStart = tim[1];
				}

				
				timeEnd = String.valueOf(calendar.get(Calendar.HOUR_OF_DAY))
						+ ":"
						+ String.valueOf(calendar.get(Calendar.MINUTE));
				
				SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");

				Date mDateStart = null, mDateEnd = null;

				try {
					mDateStart = sdf.parse(timeStart);
					mDateEnd = sdf.parse(timeEnd);
				} catch (java.text.ParseException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

				calendar.setTime(mDateStart);

				timePickerStart.setCurrentHour(calendar
						.get(Calendar.HOUR_OF_DAY));
				timePickerStart.setCurrentMinute(calendar.get(Calendar.MINUTE));

				calendar.setTime(mDateEnd);

				timePickerEnd.setCurrentHour(calendar.get(Calendar.HOUR_OF_DAY));
				timePickerEnd.setCurrentMinute(calendar.get(Calendar.MINUTE));
	
				
				timePickerStart
						.setOnTimeChangedListener(new OnTimeChangedListener() {

							@Override
							public void onTimeChanged(TimePicker view,
									int hourOfDay, int minute) {
								// TODO Auto-generated method stub
								
								if (timePickerEnd.getCurrentHour() < hourOfDay) {
									timePickerEnd.setCurrentHour(hourOfDay);
								}
//								timePickerStart.setCurrentHour(hourOfDay);
//								timePickerStart.setCurrentMinute(minute);
							}
						});

//				timePickerEnd
//						.setOnTimeChangedListener(new OnTimeChangedListener() {
//
//							@Override
//							public void onTimeChanged(TimePicker view,
//									int hourOfDay, int minute) {
//								// TODO Auto-generated method stub
//								
//							}
//						});
				
				
				AlertDialog.Builder builder = new AlertDialog.Builder(
						AddSchedule.this);
				builder.setView(timePickerView)
						.setPositiveButton("Ok",
								new DialogInterface.OnClickListener() {

									@Override
									public void onClick(DialogInterface dialog,
											int which) {
										// TODO Auto-generated method stub

										if (timePickerStart.getCurrentHour() > timePickerEnd
												.getCurrentHour()) {
											AlertDialog.Builder builder = new AlertDialog.Builder(
													AddSchedule.this);
											builder.setMessage("The end time must be greater than or equal to the start time");

											Dialog dialog2 = builder.create();
											dialog2.setCanceledOnTouchOutside(true);
											dialog2.show();
										} else if (timePickerStart
												.getCurrentHour() == timePickerEnd
												.getCurrentHour()
												&& timePickerStart
														.getCurrentMinute() > timePickerEnd
														.getCurrentMinute()) {
											AlertDialog.Builder builder = new AlertDialog.Builder(
													AddSchedule.this);
											builder.setMessage("The end time must be greater than or equal to the start time");
											Dialog dialog2 = builder.create();
											dialog2.setCanceledOnTouchOutside(true);
											dialog2.show();
										} else {

											String h, m;

											if (timePickerStart
													.getCurrentHour() < 10) {
												h = "0"
														+ timePickerStart
																.getCurrentHour();
											} else {
												h = ""
														+ timePickerStart
																.getCurrentHour();
											}

											if (timePickerStart
													.getCurrentMinute() < 10) {
												m = "0"
														+ timePickerStart
																.getCurrentMinute();
											} else {
												m = ""
														+ timePickerStart
																.getCurrentMinute();
											}

											timeStart = h + ":" + m;

											if (timePickerEnd.getCurrentHour() < 10) {
												h = "0" + timePickerEnd.getCurrentHour();
											} else {
												h = ""+ timePickerEnd.getCurrentHour();
											}

											if (timePickerEnd
													.getCurrentMinute() < 10) {
												m = "0"
														+ timePickerEnd
																.getCurrentMinute();
											} else {
												m = ""
														+ timePickerEnd
																.getCurrentMinute();
											}

											timeEnd = h + ":" + m;

											if (listTime.contains(timeStart
													+ " - " + timeEnd)) {
												AlertDialog.Builder builder = new AlertDialog.Builder(
														AddSchedule.this);
												builder.setMessage("This time points has existed !");
												Dialog dialog2 = builder
														.create();
												dialog2.setCanceledOnTouchOutside(true);
												dialog2.show();

											} else {
												listTime.add(timeStart + " - "
														+ timeEnd);

											}

											Collections.sort(listTime);

											adapter.notifyDataSetChanged();
										}
									}
								})
						.setNegativeButton("Cancel",
								new DialogInterface.OnClickListener() {

									@Override
									public void onClick(DialogInterface dialog,
											int which) {
										// TODO Auto-generated method stub

									}
								});

				Dialog dialog = builder.create();
				dialog.setCanceledOnTouchOutside(true);
				dialog.show();
			}
		});

		tvDay.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				AlertDialog.Builder builder = new AlertDialog.Builder(
						AddSchedule.this);

				View checkBoxView = View.inflate(getApplicationContext(),
						R.layout.layout_checkbox_schedule, null);
				cbMonday = (CheckBox) checkBoxView.findViewById(R.id.cbActive);
				cbTuesday = (CheckBox) checkBoxView
						.findViewById(R.id.checkBox2);
				cbWednesday = (CheckBox) checkBoxView
						.findViewById(R.id.checkBox3);
				cbThurday = (CheckBox) checkBoxView
						.findViewById(R.id.checkBox4);
				cbFriday = (CheckBox) checkBoxView.findViewById(R.id.checkBox5);
				cbSaturday = (CheckBox) checkBoxView
						.findViewById(R.id.checkBox6);
				cbSunday = (CheckBox) checkBoxView.findViewById(R.id.checkBox7);

				cbAll = (CheckBox) checkBoxView.findViewById(R.id.checkBox8);

				cbMonday.setChecked(isMon);
				cbTuesday.setChecked(isTue);
				cbWednesday.setChecked(isWed);
				cbThurday.setChecked(isThur);
				cbFriday.setChecked(isFri);
				cbSaturday.setChecked(isSat);
				cbSunday.setChecked(isSun);
				cbAll.setChecked(isAll);

				cbMonday.setOnCheckedChangeListener(new OnCheckedChangeListener() {

					@Override
					public void onCheckedChanged(CompoundButton buttonView,
							boolean isChecked) {
						// TODO Auto-generated method stub
						if (!isChecked) {
							cbAll.setChecked(false);
						}
					}
				});

				cbTuesday
						.setOnCheckedChangeListener(new OnCheckedChangeListener() {

							@Override
							public void onCheckedChanged(
									CompoundButton buttonView, boolean isChecked) {
								// TODO Auto-generated method stub
								if (!isChecked) {
									cbAll.setChecked(false);
								}
							}
						});
				cbWednesday
						.setOnCheckedChangeListener(new OnCheckedChangeListener() {

							@Override
							public void onCheckedChanged(
									CompoundButton buttonView, boolean isChecked) {
								// TODO Auto-generated method stub
								if (!isChecked) {
									cbAll.setChecked(false);
								}
							}
						});
				cbThurday
						.setOnCheckedChangeListener(new OnCheckedChangeListener() {

							@Override
							public void onCheckedChanged(
									CompoundButton buttonView, boolean isChecked) {
								// TODO Auto-generated method stub
								if (!isChecked) {
									cbAll.setChecked(false);
								}
							}
						});
				cbFriday.setOnCheckedChangeListener(new OnCheckedChangeListener() {

					@Override
					public void onCheckedChanged(CompoundButton buttonView,
							boolean isChecked) {
						// TODO Auto-generated method stub
						if (!isChecked) {
							cbAll.setChecked(false);
						}
					}
				});
				cbSaturday
						.setOnCheckedChangeListener(new OnCheckedChangeListener() {

							@Override
							public void onCheckedChanged(
									CompoundButton buttonView, boolean isChecked) {
								// TODO Auto-generated method stub
								if (!isChecked) {
									cbAll.setChecked(false);
								}
							}
						});
				cbSunday.setOnCheckedChangeListener(new OnCheckedChangeListener() {

					@Override
					public void onCheckedChanged(CompoundButton buttonView,
							boolean isChecked) {
						// TODO Auto-generated method stub
						if (!isChecked) {
							cbAll.setChecked(false);
						}
					}
				});

				cbAll.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						isAll = !isAll;

						cbMonday.setChecked(isAll);
						cbTuesday.setChecked(isAll);
						cbWednesday.setChecked(isAll);
						cbThurday.setChecked(isAll);
						cbFriday.setChecked(isAll);
						cbSaturday.setChecked(isAll);
						cbSunday.setChecked(isAll);
					}
				});

				builder.setView(checkBoxView);
				builder.setTitle("Day")
						.setCancelable(true)
						.setPositiveButton("Ok",
								new DialogInterface.OnClickListener() {
									public void onClick(DialogInterface dialog,
											int id) {

										if (cbAll.isChecked()) {
											isAll = true;
											isMon = true;
											isTue = true;
											isWed = true;
											isThur = true;
											isFri = true;
											isSat = true;
											isSun = true;
										} else {
											isMon = cbMonday.isChecked();
											isTue = cbTuesday.isChecked();
											isWed = cbWednesday.isChecked();
											isThur = cbThurday.isChecked();
											isFri = cbFriday.isChecked();
											isSat = cbSaturday.isChecked();
											isSun = cbSunday.isChecked();

											isAll = false;

											if (isMon && isTue && isWed
													&& isThur && isFri && isSat
													&& isSun) {
												isAll = true;
											}
										}

										changeStatusOfDate();
									}
								})
						.setNegativeButton("Cancel",
								new DialogInterface.OnClickListener() {

									@Override
									public void onClick(DialogInterface dialog,
											int which) {
										// TODO Auto-generated method stub

									}
								});

				Dialog aDialog = builder.create();
				aDialog.setCanceledOnTouchOutside(true);
				aDialog.show();
			}
		});

		toggleActivity
				.setOnCheckedChangeListener(new OnCheckedChangeListener() {

					@Override
					public void onCheckedChanged(CompoundButton buttonView,
							boolean isChecked) {
						// TODO Auto-generated method stub
						isActivity = isChecked;
					}
				});

		toggleRepeat.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				// TODO Auto-generated method stub
				isRepeat = isChecked;
			}
		});

		btSave.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub

				if (newTask == null) {
					newTask = new Timer();
				}

				if (listTime == null || listTime.size() < 1) {
					AlertDialog.Builder builder = new AlertDialog.Builder(
							AddSchedule.this);
					builder.setMessage("Please set up time for task");
					Dialog dialog = builder.create();
					dialog.setCanceledOnTouchOutside(true);
					dialog.show();
				} else {

					int countActvi = 0;
								
					if (dates.equalsIgnoreCase("")) {
						isActivity = true;
						isRepeat = false;
						Calendar cld = Calendar.getInstance();

						String tme = String.valueOf(cld
								.get(Calendar.HOUR_OF_DAY))
								+ ":"
								+ String.valueOf(cld.get(Calendar.MINUTE));
						String timeTask = listTime.get(listTime.size() - 1)
								.split(" - ")[1];

						int compare = timeTask.compareToIgnoreCase(tme);

						int d = cld.get(Calendar.DAY_OF_WEEK);
						switch (d) {
						case 1:
							if (compare <= 0) {
								dates += "Mon ";
							} else {
								dates += "Sun ";
							}
							break;
						case 2:
							if (compare <= 0) {
								dates += "Tue ";
							} else {
								dates += "Mon ";
							}
							break;
						case 3:
							if (compare <= 0) {
								dates += "Wed ";
							} else {
								dates += "Tue ";
							}
							break;
						case 4:
							if (compare <= 0) {
								dates += "Thu ";
							} else {
								dates += "Wed ";
							}
							break;
						case 5:
							if (compare <= 0) {
								dates += "Fri ";
							} else {
								dates += "Thu ";
							}
							break;
						case 6:
							if (compare <= 0) {
								dates += "Sat ";
							} else {
								dates += "Fri ";
							}
							break;
						case 7:
							if (compare <= 0) {
								dates += "Sun ";
							} else {
								dates += "Mon ";
							}
							break;

						default:
							break;
						}
					}

					
					if (dates.contains("Mon")) {
						countActvi += 1;
					}
					if (dates.contains("Tue")) {
						countActvi += 1;
					}
					if (dates.contains("Wed")) {
						countActvi += 1;
					}
					if (dates.contains("Thu")) {
						countActvi += 1;
					}
					if (dates.contains("Fri")) {
						countActvi += 1;
					}
					if (dates.contains("Sat")) {
						countActvi += 1;
					}
					if (dates.contains("Sun")) {
						countActvi += 1;
					}
					
					newTask.setDates(dates);
					newTask.setActive(isActivity == true ? countActvi : 0);
					
					newTask.setRepeat(isRepeat == true ? 1 : 0);
					String time = "";
					for (String s : listTime) {
						time += s + "__";
					}

					newTask.setTime(time.substring(0, time.length() - 2));

					AlertDialog.Builder builderConfirm = new AlertDialog.Builder(
							AddSchedule.this);

					final EditText name = new EditText(mContext);

					name.setTextColor(getResources().getColor(
							R.color.main_color));

					if (typeSchedule == MainActivity.REQUEST_CODE_EDT_SCHEDULE) {
						name.setText(newTask.getNameTask());
					} else {
						name.setText("Task ");
					}

					builderConfirm
							.setTitle("Name for task")
							.setView(name)
							.setPositiveButton("Done",
									new DialogInterface.OnClickListener() {

										@Override
										public void onClick(
												DialogInterface dialog,
												int which) {
											// TODO Auto-generated method stub

											newTask.setNameTask(name.getText()
													.toString());

											if (dbHandler == null) {
												dbHandler = new DatabaseHandler(
														mContext);
											}

											if (typeSchedule == MainActivity.REQUEST_CODE_EDT_SCHEDULE) {
												dbHandler.updateTimer(newTask);
											} else {
												dbHandler.insertTimer(newTask);
											}

											setResult(RESULT_OK);

											finish();

										}
									})
							.setNegativeButton("Cancel",
									new DialogInterface.OnClickListener() {

										@Override
										public void onClick(
												DialogInterface dialog,
												int which) {
											// TODO Auto-generated method stub

										}
									});

					Dialog dialogConfirm = builderConfirm.create();
					dialogConfirm.show();
				}
			}
		});

		btCancel.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				setResult(RESULT_CANCELED);
				finish();
			}
		});

	}

	private void findViewByid() {
		toggleActivity = (ToggleButton) findViewById(R.id.toggleActivitySchedule);

		toggleRepeat = (ToggleButton) findViewById(R.id.toggleRepeatTiming);

		tvTime = (TextView) findViewById(R.id.tvTimeNewTask);

		tvDay = (TextView) findViewById(R.id.tvDate);

		lvTimer = (ListView) findViewById(R.id.lvTimer);

		btCancel = (Button) findViewById(R.id.bt_cancel_schedule);

		btSave = (Button) findViewById(R.id.bt_save_schedule);

		tvMon = (TextView) findViewById(R.id.tvMonday);

		tvTue = (TextView) findViewById(R.id.tvTue);

		tvWed = (TextView) findViewById(R.id.tvWed);

		tvThur = (TextView) findViewById(R.id.tvThur);

		tvFri = (TextView) findViewById(R.id.tvFri);

		tvSat = (TextView) findViewById(R.id.tvSat);

		tvSun = (TextView) findViewById(R.id.tvSun);

	}

	private void changeStatusOfDate() {
		dates = "";
		if (isMon) {
			dates += "Mon ";
			tvMon.setTextColor(getResources().getColor(android.R.color.white));
		} else {
			tvMon.setTextColor(getResources().getColor(R.color.black_subsub));
		}

		if (isTue) {
			dates += "Tue ";
			tvTue.setTextColor(getResources().getColor(android.R.color.white));
		} else {
			tvTue.setTextColor(getResources().getColor(R.color.black_subsub));
		}

		if (isWed) {
			dates += "Wed ";
			tvWed.setTextColor(getResources().getColor(android.R.color.white));
		} else {
			tvWed.setTextColor(getResources().getColor(R.color.black_subsub));
		}

		if (isThur) {
			dates += "Thu ";
			tvThur.setTextColor(getResources().getColor(android.R.color.white));
		} else {
			tvThur.setTextColor(getResources().getColor(R.color.black_subsub));
		}

		if (isFri) {
			dates += "Fri ";
			tvFri.setTextColor(getResources().getColor(android.R.color.white));
		} else {
			tvFri.setTextColor(getResources().getColor(R.color.black_subsub));
		}

		if (isSat) {
			dates += "Sat ";
			tvSat.setTextColor(getResources().getColor(android.R.color.white));
		} else {
			tvSat.setTextColor(getResources().getColor(R.color.black_subsub));
		}

		if (isSun) {
			dates += "Sun ";
			tvSun.setTextColor(getResources().getColor(android.R.color.white));
		} else {
			tvSun.setTextColor(getResources().getColor(R.color.black_subsub));
		}
	}

	@Override
	public void onResume() {
		super.onResume();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.add_schedule, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	public class ItemTimeTask extends BaseAdapter {

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return listTime.size();
		}

		@Override
		public Object getItem(int arg0) {
			// TODO Auto-generated method stub
			return listTime.get(arg0);
		}

		@Override
		public long getItemId(int arg0) {
			// TODO Auto-generated method stub
			return arg0;
		}

		@Override
		public View getView(final int position, View convertView, ViewGroup arg2) {
			// TODO Auto-generated method stub

			LayoutInflater inflater = (LayoutInflater) mContext
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

			ViewHolderTask viewHolder = new ViewHolderTask();

			if (convertView == null) {

				convertView = inflater.inflate(R.layout.item_time_task, null);

				viewHolder.tvTime = (TextView) convertView
						.findViewById(R.id.tvTimeTask);

				viewHolder.imgBtDel = (ImageButton) convertView
						.findViewById(R.id.bt_delete_time_task);

				convertView.setTag(viewHolder);

			} else {
				viewHolder = (ViewHolderTask) convertView.getTag();
			}

			viewHolder.tvTime.setText(listTime.get(position));

			viewHolder.imgBtDel.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					listTime.remove(position);
					adapter.notifyDataSetChanged();
				}
			});

			return convertView;
		}

	}

	static class ViewHolderTask {

		ImageButton imgBtDel;
		TextView tvTime;

	}
}
